package com.example.seckill.exception;

import com.example.seckill.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @描述 全局异常处理器
 * @码农 丁昌江
 * @日期 2021/8/10 10:58
 * @返回
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理 Exception 异常
     *
     * @param httpServletRequest httpServletRequest
     * @param e                  异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Response<Void> exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
        e.printStackTrace();
        return Response.getNoBody(500,"服务器异常");
    }

    /**
     * 处理 BusinessException 异常
     *
     * @param httpServletRequest httpServletRequest
     * @param e                  异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public Response<Void> businessExceptionHandler(HttpServletRequest httpServletRequest, BusinessException e) {
        //logger.info("业务异常。code:" + e.getCode() + "msg:" + e.getMsg());
        return Response.getNoBody(e.getCode(), e.getMsg());
    }
}
